package edu.szte.ovrt.datahandler.ui.console;

import java.io.FileNotFoundException;

import edu.szte.ovrt.datahandler.core.*;
import edu.szte.ovrt.datahandler.model.*;

/**
 * Menu state for {@link Document} level.
 * 
 * @author Péter Török
 */
public class EditDocument extends Menu {
	protected Document m_document;
	protected String m_fileName;

	public EditDocument(final Document document) {
		this(document, "");
	}

	public EditDocument(final Document document, final String fileName) {
		m_document = document;
		m_fileName = fileName;
		initialize();
	}

	@Override
	protected String getTitle() {
		return "Document";
	}

	protected void initialize() {
		m_menuMap.clear();
		m_menuMap.add("List content", new IAction() {
			@Override
			public void run() throws ExitAction {
				newScreen("Content");
				for (final Section s : m_document.getSections()) {
					System.out.printf("[%s]\n", s.getName());
					for (final Item i : s.getItems()) {
						System.out.printf("   %s = %s\n", i.getKey(), i.getValue());
					}
				}
				showMessage("Press [ENTER] key to continue.");
				readInt();
			}
		});

		m_menuMap.add("New section", new IAction() {
			public void run() {
				final String newSectionName = (new Dialog("Name of new section", true)).getValue();
				if (newSectionName.length() != 0) {
					final Section section = m_document.newSection(newSectionName);
					final EditSection editor = new EditSection(section);
					editor.run();
				} else {
					showError("The section's name cannot be empty.");
				}
			}
		});

		m_menuMap.add("Edit section", new IAction() {
			@Override
			public void run() {
				try {
					final Section s = (new Selector<Section>(m_document.getSections())).getSelection();
					(new EditSection(s)).run();
				} catch (final ExitAction e) {
					// NOOP
				}
			}
		});

		m_menuMap.add("Delete section", new IAction() {

			@Override
			public void run() {
				try {
					final Section s = (new Selector<Section>(m_document.getSections())).getSelection();
					if (m_document.getSections().remove(s)) {
						showMessage("Successfully deleted.");
					} else {
						showError("Deletion failed.");
					}
				} catch (final ExitAction e) {
					// NOOP
				}
			}
		});

		m_menuMap.add("Save", new IAction() {
			@Override
			public void run() throws ExitAction {
				save(m_fileName);
			}
		});

		m_menuMap.add("Save As", new IAction() {

			@Override
			public void run() throws ExitAction {
				final String filename = (new Dialog("filename", true)).getValue();

				save(filename);

			}
		});

		m_menuMap.add("Exit", new IAction() {
			@Override
			public void run() throws ExitAction {
				throw ExitAction.getInstance();
			}
		});
	}

	void save(final String filename) {
		try {
			final IOFactory factory = IOMetaFactory.getInstance().getIOFactory(filename);
			if (factory == null) { throw new DocumentWriterException("File extension not supported!"); }
			final DocumentWriter wr = factory.createDocumentWriter();
			wr.write(m_document);

		} catch (final FileNotFoundException e) {
			showError("File not found!");
		} catch (final DocumentWriterException e) {
			if (e.getCause() != null) {
				showError(e.getCause().getMessage());
			} else {
				showError(e.getMessage());
			}
		}
	}
}
